package com.sinog.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.basic.mapper.MenuMapper;
import com.sinog.basic.mapper.ResourceMapper;
import com.sinog.basic.mapper.UserMapper;
import com.sinog.basic.service.MenuService;
import com.sinog.core.domain.Resource;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @desc 获取公共页面的资源
 * @author zy
 * @date -
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Resource> implements MenuService {

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * LIST页组件
     * @param account 账号
     * @param id 资源id
     * @param state state
     * @param level level
     * @return Result
     */
    @Override
    public Result selectListPageComponent(String account,Long id,String state,String level) {
        List<String> roles = userMapper.selectUserRoles(account);
        if(null == roles || roles.isEmpty()) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        List<Resource> resources = resourceMapper.selectResouce(roles,id);
        if(null == resources || resources.isEmpty()) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        Map<String,Object> result = new HashMap<>(16);
        Resource bean = new Resource();
        // 自己
        Resource selfResource = bean.selectOne(new QueryWrapper<Resource>().eq("id",id));
        //做判断更改
        if(StringUtils.isNotBlank(state)) {
             //衔接
            if(state.startsWith(Constants.StrFinalValue.SMALLX) || Constants.StrFinalValue.W4.equals(state)) {
                selfResource.setOutjs("azbj/yazgl/ysf_xj.js");
            //迁移
            } else if(state.startsWith(Constants.StrFinalValue.SMALLQ)) {
                selfResource.setOutjs("azbj/yazgl/yazhjqy.js");
            //双列管
            } else if(state.startsWith(Constants.StrFinalValue.SMALLL)) {
                selfResource.setOutjs("azbj/azbjgl/slg.js");
            //解除帮教
            } else if(Constants.StrFinalValue.W2.equals(state) || Constants.StrFinalValue.W3.equals(state)) {
                selfResource.setOutjs("azbj/jcbjgl/jcbjgl.js");
            //全部
            } else if(state.equals(Constants.NumberStr.NUM_STR_ZERO)){
                selfResource.setOutjs("azbj/work/Work.js");
            }
        }
        // 没有本权限快速返回
        if(!resources.contains(selfResource)) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        result.put("self",selfResource);
        // 子资源
        List<Resource> subResources = bean.selectList(new QueryWrapper<Resource>().eq("pid",id).orderByAsc("pid","sn"));
        // 取集合的交集控制资源的权限
        subResources.retainAll(resources);
        if(StringUtils.isNotBlank(state)) {
                //已衔接
            if(Constants.StrFinalValue.X1.equals(state)) {
                List<Resource> resourcesYxj = resourceMapper.selectresourcesHcsbResouce(roles,Long.valueOf("247177213722624"));
                subResources.addAll(resourcesYxj);
                //核查失败
            } else if(state.equals(Constants.NumberStr.NUM_STR_FOUR)) {
                List<Resource> resourcesXxbl = resourceMapper.selectresourcesHcsbResouce(roles,Long.valueOf("247553952546816"));
                subResources.addAll(resourcesXxbl);
            }
        }
        List<Resource> leftList = new ArrayList<>(16);
        List<Resource> rightList = new ArrayList<>(16);
        List<Resource> midList = new ArrayList<>(16);
        for(Resource resource : subResources) {
            Integer align = resource.getAlign();
            if(align.equals(Constants.Number.NUM_ZERO)) {
                // 左侧按钮
                leftList.add(resource);
            } else if(align.equals(Constants.Number.NUM_ONE)) {
                // 右侧的查询
                rightList.add(resource);
            } else {
                Integer type = resource.getType();
                switch(type) {
                    case 900:
                        //列表
                        if(StringUtils.isNotBlank(state)) {
                            if(Constants.StrFinalValue.W4.equals(state)) {
                                //未衔接
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/yazgl/xjxx/wxj/list?1=1&stateXj=" + state.charAt(1));
                                    }
                                });
                            } else if(state.startsWith("x")) {
                                //衔接
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/yazgl/xjxx/xxbl/list?1=1&stateXj=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.W3.equals(state)) {
                                //解除帮教
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/jcbjgl/getReleaseHelpEduList?1=1&shKey=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.W2.equals(state)) {
                                //解除帮教待审批
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/jcbjgl/getReleaseHelpEduList?1=1&shKey=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.L1.equals(state)) {
                                //双列管申请成功
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/slg/jzdlist?&key=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.L3.equals(state)) {
                                //双列管申请失败
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/slg/jzdlist?&key=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.Q1.equals(state)) {
                                //发起迁移
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/transfer/fqqy/info/list?type=" + state.charAt(1));
                                    }
                                });
                            } else if(Constants.StrFinalValue.Q2.equals(state)) {
                                //接收迁移
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/transfer/fqqy/info/list?type=" + state.charAt(1));
                                    }
                                });
                            } else {//核查
                                subResources.forEach(re -> {
                                    if("列表".equals(re.getName())) {
                                        re.setAction("/azbj/verification/info/work?state=" + state);
                                    }
                                });
                            }
                        }
                        // Grid列表子资源
                        result.put("grid",resource);
                        List<Resource> fieldResources = bean.selectList(new QueryWrapper<Resource>().eq("pid",resource.getId())
                                                                                                    .eq("type",950)
                                                                                                    .orderByAsc("sn"));
                        //对操作做判断
                        if(StringUtils.isNotBlank(state)) {
                            switch(state) {
                                //双列管申请成功
                                case "l1":
                                //双列管申请失败
                                case "l3":
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operator");
                                            re.setExtension("renderer=\"onHjqcSuoActionRendererJz\"");
                                        }
                                    });
                                    break;
                                case "w3":
                                case "w2":
                                    //解除帮教待审批
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("showstate");
                                            re.setExtension("renderer=\"onJcbj\"");
                                        }
                                    });
                                    break;
                                case "w4":
                                    //未衔接
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("flag");
                                            re.setExtension("renderer=\"onWxjSuoActionRenderer\"");
                                        }
                                    });
                                    break;
                                //已衔接
                                case "x1":
                                //核查失败
                                case "4":
                                    fieldResources.remove(fieldResources.size() - 1);
                                    break;
                                case "2":
                                    //核查中
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operateStateHc");
                                            re.setExtension("renderer=\"autoOperateChecking\"");
                                        }
                                    });
                                    break;
                                case "8":
                                    //待比对
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operateStateHc");
                                            re.setExtension("renderer=\"autoOperateDbd\"");
                                        }
                                    });
                                    break;
                                case "3":
                                    //核查成功
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operateStateHc");
                                            re.setExtension("renderer=\"autoSuccessOperateChecking\"");
                                        }
                                    });
                                    break;
                                case "5":
                                    //二次核查成功
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField(null);
                                            re.setExtension("renderer=\"autoOperateSecondHcSuccess\"");
                                        }
                                    });
                                    break;
                                case "x2":
                                    //衔接中
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("xjflag");
                                            re.setExtension("renderer=\"onXjActionRenderer\"");
                                        }
                                    });
                                    break;
                                case "x3":
                                    //衔接失败
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("secondstate");
                                            re.setExtension("renderer=\"onHjqrSuoActionRenderer\"");
                                        }
                                    });
                                    break;
                                case "q1":
                                    //发起迁移
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operate");
                                            re.setExtension("renderer=\"onFqqyActionRenderer\"");
                                        }
                                    });
                                    break;
                                case "q2":
                                    //接收迁移
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operate");
                                            re.setExtension("renderer=\"onJsqyActionRenderer\"");
                                        }
                                    });
                                    break;
                                case "x4":
                                    //信息补录
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField(null);
                                            re.setExtension("renderer=\"autoOperateXxbl\"");
                                        }
                                    });
                                    break;
                                case "x5":
                                    //待衔接
                                    fieldResources.forEach(re -> {
                                        if("操作".equals(re.getName())) {
                                            re.setField("operateStateDxj");
                                            re.setExtension("renderer=\"autoOperateControl\"");
                                        }
                                    });
                                    break;
                                default:
                                    result.put("fieldList",fieldResources);
                                    break;
                            }
                        }
                        result.put("fieldList",fieldResources);
                        // 取集合的交集控制资源的权限
                        fieldResources.retainAll(resources);
                        List<Resource> fieldResources1 = new ArrayList<>(1);
                        for (Iterator iter = fieldResources.iterator(); iter.hasNext();) {
                            Resource resource1 = (Resource)iter.next();
                            if("操作".equals(resource1.getName().trim())){
                                fieldResources1.add(resource1);
                                iter.remove();
                            }
                        }
                        result.put("fieldList",fieldResources);
                        result.put("fieldList1",fieldResources1);
                        break;
                    case 960:
                        // 列中的按钮
                        midList.add(resource);
                        break;
                    default:
                }
            }
        }
        result.put("leftList",leftList);
        //根据角色，剔除赛选条件
        if(StringUtils.isNotBlank(state)) {
            Iterator<Resource> iterator = rightList.iterator();
            //其他条件
            if(level.equals(Constants.OrgLevel.LEVEL_SFS) || level.equals(Constants.OrgLevel.LEVEL_QXSFJ) || level.equals(Constants.OrgLevel.LEVEL_DSSFJ) || level.equals(Constants.OrgLevel.LEVEL_SFT)) {
                while(iterator.hasNext()) {
                    Resource next = iterator.next();
                    if("其他条件1".equals(next.getName())) {
                        iterator.remove();
                    }
                }
            } else {//司法所及之上部门
                while(iterator.hasNext()) {
                    Resource next = iterator.next();
                    if("其他条件2".equals(next.getName())) {
                        iterator.remove();
                    }
                }
            }
        }
        result.put("rightList",rightList);
        if(StringUtils.isNotBlank(state)) {
            //已衔接
            if(Constants.StrFinalValue.X1.equals(state)) {
                midList.forEach(mid -> {
                    if(Constants.StrFinalValue.VIEW.equals(mid.getName())) {
                        mid.setAction("rowdblclick()");
                    }
                    if(Constants.StrFinalValue.EXPORT.equals(mid.getName())) {
                        mid.setAction("exportfiles()");
                    }
                    if(Constants.StrFinalValue.FJDIR.equals(mid.getName())) {
                        mid.setAction("addfiles()");
                    }
                });
                result.put("midList",midList);
            } else {
                result.put("midList",midList);
            }
        } else {
            result.put("midList",midList);
        }
        return Result.success(result);
    }

    /**
     * TAB页组件
     * @param account account
     * @param id id
     * @return Result
     */
    @Override
    public Result selectTabPageComponent(String account,Long id) {
        List<String> roles = userMapper.selectUserRoles(account);
        if(null == roles || roles.isEmpty()) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        List<Resource> resources = resourceMapper.selectResouce(roles,id);
        if(null == resources || resources.isEmpty()) {
            return Result.failure(ReturnCode.SELECT_FAILED);
        }
        Resource bean = new Resource();
        //子资源
        List<Resource> tabResources = bean.selectList(new QueryWrapper<Resource>().eq("pid",id).eq("type",300)
                                                                                  .orderByAsc("sn"));
        tabResources.retainAll(resources);
        if(!tabResources.isEmpty()) {
            return Result.success(tabResources);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}